﻿using Microsoft.EntityFrameworkCore;
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using wt.wine.Application.Common.Interfaces;
using wt.wine.Domain.Entities;

namespace wt.wine.Infrastructure.Persistence.Repository
{
    public class RoletableRepository: IRoletableRepository
    {
        private readonly IApplicationDbContext _dbcontext;
        public RoletableRepository(IApplicationDbContext dbcontext)
        {
            _dbcontext = dbcontext;
        }
        public async Task<roletable> GetRoleTableById(int id, CancellationToken cancellationtoken)
        {
            return await _dbcontext.roletable.AsNoTracking().Where(p => p.parentid == id && p.state >= 0).OrderByDescending(p => p.id).FirstOrDefaultAsync(cancellationtoken);
        }

        /// <summary>
        /// 通过角色id获取角色信息
        /// </summary>
        /// <param name="id"></param>
        /// <param name="cancellationtoken"></param>
        /// <returns></returns>
        public async Task<roletable> GetRoleTableByRoleId(int id, CancellationToken cancellationtoken)
        {
            return await _dbcontext.roletable.AsNoTracking().Where(p => p.id == id && p.state >= 0).OrderByDescending(p => p.id).FirstOrDefaultAsync(cancellationtoken);
        }
    }
}
